package cn.wgx.modules.sys.services;

import cn.wgx.commons.util.DateUtils;
import cn.wgx.modules.base.services.BaseService;
import cn.wgx.modules.sys.entity.SysLog;
import cn.wgx.modules.sys.entity.SysLogExample;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.entity.dto.DTOSysLog;
import cn.wgx.modules.sys.entity.mapper.SysLogMapper;
import cn.wgx.modules.sys.util.UserUtil;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SysLogService extends BaseService<SysLog> {

    @Resource
    SysLogMapper sysLogMapper;

    @Transactional
    public List<SysLog> getList(Integer userId, Date startDate, Date endDate) {
        SysLogExample example = new SysLogExample();
        SysLogExample.Criteria criteria = example.createCriteria();
        if (userId != null) {
            criteria.andCreateUserIdEqualTo(userId);
        }
        if(startDate != null){
            criteria.andCreateDateGreaterThanOrEqualTo(startDate);
        }
        if(endDate != null){
            criteria.andCreateDateLessThan(endDate);
        }
        example.setOrderByClause("log_id desc");
        List<SysLog> logs = sysLogMapper.selectByExample(example);
        if(logs != null && logs.size() > 0){
            List<DTOSysLog> dtoLogs = turnDTO(logs);
            addUsers(dtoLogs);
        }
        return logs;
    }

    @Transactional(readOnly = true)
    public List<SysLog> getListByIds(List<Integer> ids) {
        if(ids == null || ids.isEmpty()){
            return emptyList;
        }
        List<Integer> uniList = getUniList(ids);
        if(!uniList.isEmpty()){
            SysLogExample example = new SysLogExample();
            SysLogExample.Criteria criteria = example.createCriteria();
            criteria.andLog_idIn(uniList);
            List<SysLog> eduCourseAssignmentHis = sysLogMapper.selectByExample(example);
            return eduCourseAssignmentHis;
        }
        return null;
    }


    public List<DTOSysLog> turnDTO(List<SysLog> sysLogs){
        List<DTOSysLog> list = new ArrayList<>();
        for(SysLog sysLog: sysLogs){
            DTOSysLog dtoSysLog = turnDTO(sysLog);
            list.add(dtoSysLog);
        }
        return  list;
    }

    public DTOSysLog turnDTO(SysLog sysLog){
        DTOSysLog dtoSysLog = new DTOSysLog();
        org.springframework.beans.BeanUtils.copyProperties(sysLog, dtoSysLog);
        return  dtoSysLog;
    }
    public void addUsers(List<DTOSysLog> logs){
        Set<Integer> ids = new HashSet();
        for(DTOSysLog sysLog : logs){
            ids.add(sysLog.getCreateUserId());
        }
        List<SysUser> users = UserUtil.getUsersByIds(ids);
        for(DTOSysLog sysLog : logs){
            Integer userId = sysLog.getCreateUserId();
            for(SysUser sysUser: users){
                if(userId.equals(sysUser.getUser_id())){
                    try {
                        sysLog.set_sysUser((SysUser)BeanUtils.cloneBean(sysUser));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                }
            }
        }
    }

    /**
     * 获取今日活跃用户数
     * @return
     */
    @Transactional(readOnly = true)
    public int getTodayActiveUserCount(){
        int c = sysLogMapper.countTodayUser(DateUtils.getDateZero());
        return c;
    }

    @Transactional(readOnly = true)
    public List<Map> getCountActive(Date startDate, Date endDate){
        List<Map> map = sysLogMapper.countActive(startDate, endDate);
        return map;
    }

    @Transactional
    public SysLog insertOne(SysLog record) {
        sysLogMapper.insertSelective(record);
        return record;
    }


    @Transactional
    public List<SysLog> insertBatch(List<SysLog> records) {
        for (SysLog record : records) {
            sysLogMapper.insertSelective(record);
        }
        return records;
    }

    @Transactional
    public int updateOne(SysLog record) {
        return sysLogMapper.updateByPrimaryKey(record);

    }

    @Transactional
    public long updateBatch(List<SysLog> records) {
        int count = 0;
        for (SysLog record : records) {
            count += sysLogMapper.updateByPrimaryKey(record);
        }
        return count;
    }

    @Transactional
    public int deleteOne( Integer id) {
        return sysLogMapper.deleteByPrimaryKey(id);
    }


    @Transactional
    public int deleteBatch(List<Integer> ids) {
        int count = 0;
        for (Integer id : ids) {
            count += sysLogMapper.deleteByPrimaryKey(id);
        }
        return count;
    }

}